from flask import Flask, jsonify, request
from flask_cors import CORS
import uuid
app = Flask(__name__)
CORS(app)

# 假设您有一个包含学生信息的字典列表
students = [
    {"id": 1, "name": "Alice", "class": "A"},
    {"id": 2, "name": "Bob", "class": "B"},
    {"id": 3, "name": "Charlie", "class": "C"}
]



# 获取学生信息的接口
@app.route('/students', methods=['GET'])
def get_students():
    student_id = request.args.get('id')
    if student_id:
        student = next((student for student in students if student["id"] == int(student_id)), None)
        if student:
            return jsonify(student)
        else:
            return jsonify({"error": "Student not found"}), 404
    else:
        return jsonify(students)

# 创建新学生信息的接口
@app.route('/students/new', methods=['post'])
def create_student():
    data = request.json
    print(data)
    new_student = {
        "id": str(uuid.uuid4()),
        "name": data["name"],
        "class": data["class"]
    }

    students.append(new_student)
    return jsonify({"message": "Student created successfully"}), 201


if __name__ == '__main__':
    app.run(debug=True)
